import Vue from 'vue'
import Router from 'vue-router'

import home from '@/components/home'
import document from '@/components/document'
import about from '@/components/about'
import user from '@/components/user'
import error from '@/components/404'
import vuex from '@/components/vuex'
import axios from '@/components/axios'

import study from '@/views/study'
import work from '@/views/work'
import hobby from '@/views/hobby'


Vue.use(Router)


let router =  new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: home,
      meta:{
        index: 0
      }
    },
    {
      path: '/user',
      name: 'user',
      component: user,
      meta:{
        index: 1
      },
      // beforeEnter(to,from,next){
      //   console.log('组件钩子函数 beforeEach')
      //   next()
      // }
    },
    {
      path: '/about',
      component: about,
      children:[
        {
          path: '', // 默认的子路由  /about
          name: 'About',
          component: study,
          meta:{
            index: 2
          }
        },
        {
          path: '/about/work',  // /work
          name: 'Work',
          component: work
        },
        {
          path: '/about/hobby', // /hobby
          name: 'Hobby',
          component: hobby
        }
      ]
    },
    {
      path: '/document',
      name: 'document',
      component: document,
      meta:{
        index: 3,
        title:'document'
      }
    },
    {
      path: '/vuex',
      component: vuex,
    },
    {
      path: '/axios',
      component: axios,
    },
    
    {
      path: '*',
      redirect:'/error'
    }
  ],

  scrollBehavior(to,from,scrollBehavior){
    if(to.hash){
      return {
        selector:to.hash
      } 
   }

    /*if(savePosition){
      return savePosition;
    }else{
      return {x:0,y:0}
    }*/

  }
})
export default  router

// router.beforeEach((to,from,next)=>{
//   console.log('beforeEach');
//   next()
//   // next(false)
// })

// router.afterEach((to,from)=>{
//   if(to.meta.title){
//       window.document.title = to.meta.title
//   }else{
//       window.document.title = ''
//   }
// })



